package com.amon.amonplayer.player.widget;

import android.content.Context;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;
import android.graphics.SurfaceTexture;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Surface;

import com.amon.amonplayer.opengl.BaseGLSurfaceView;
import com.amon.amonplayer.player.AmonPlayer;
import com.amon.amonplayer.player.MediaErrorListener;
import com.amon.amonplayer.player.MediaPreparedListener;
import com.amon.amonplayer.player.VideoInfoListener;
import com.amon.amonplayer.player.VideoRender;

import java.io.FileDescriptor;

public class GLVideoView extends BaseGLSurfaceView implements
        MediaPreparedListener, MediaErrorListener, VideoRender.RenderListener,
        VideoInfoListener {
    private final AmonPlayer mAmonPlayer;
    private final AssetManager mAssetManager;
    private Context mContext;
    private SurfaceTexture mSurfaceTexture;
    private VideoRender mVideoRender;

    private static Handler mHandler;

    private int mWidth = 0;
    private int mHeight = 0;
    private int mVideoWidth = 0;
    private int mVideoHeight = 0;

    public GLVideoView(Context context) {
        this(context, null);
    }

    public GLVideoView(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        mVideoRender = new VideoRender(context);
        setRenderer(mVideoRender);
        mVideoRender.setOnRenderListener(this);
        mAssetManager = context.getAssets();
        mHandler = new Handler(context.getMainLooper());
        mAmonPlayer = new AmonPlayer(context);
        mAmonPlayer.setOnPreparedListener(this);
        mAmonPlayer.setOnErrorListener(this);
        mAmonPlayer.setOnVideoInfoListener(this);
    }

    public void play(String uri) {
        stop();
        mAmonPlayer.setDataSource(uri);
        mAmonPlayer.prepareAsync();
    }

    public void playFd(FileDescriptor fd) {
        try {
            stop();
            mAmonPlayer.setDataSource(fd);
            mAmonPlayer.prepareAsync();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void playFd(String uri) {
        try {
            stop();
            AssetFileDescriptor afd = mAssetManager.openFd(uri);
            mAmonPlayer.setDataSource(afd.getFileDescriptor());
            mAmonPlayer.prepareAsync();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void pause() {
        mAmonPlayer.pause();
    }

    public void stop() {
        mAmonPlayer.stop();
    }

    @Override
    public void onPrepare() {
        mHandler.post(() -> {
            mAmonPlayer.setSurface(new Surface(mSurfaceTexture));
            mAmonPlayer.play();
        });
    }

    @Override
    public void onError(int code, String msg) {

    }

    @Override
    public void onSurfaceCreated(SurfaceTexture surfaceTexture) {
        mSurfaceTexture = surfaceTexture;
    }

    @Override
    public void onSurfaceDestroy() {
        stop();
    }

    @Override
    public void onSizeInfo(int width, int height) {
        Log.i("TAG", "width:" + width + " height:" + height);
//        mVideoWidth = width;
//        mVideoHeight = height;
//        mHandler.post(() -> {
//            ViewGroup.LayoutParams params = getLayoutParams();
//            params.width = mWidth;
//            params.height = mWidth * mVideoHeight / mVideoWidth;
//            mSurfaceTexture.setDefaultBufferSize(params.width, params.height);
//            requestLayout();
//        });
    }
}
